from pydantic import BaseModel
from typing import Any, Optional, Literal


class JsonData(BaseModel):
    # 通用响应码
    code:int = 0
    data: Optional[Any] = None
    msg:Optional[str]  = "success"
    type:Literal["stream","text"] = "stream"


    @classmethod
    def success(cls,data:Any=None) -> "JsonData":
        return cls(code=0,data=data,type="text")

    @classmethod
    def error(cls,code:int=-1,msg:str="error") -> "JsonData":
        return cls(code=code,msg=msg,type="text")

    @classmethod
    def stream_data(cls,data:Any=None,msg:str="")->"JsonData":
        return cls(code=0,data=data,msg=msg,type="stream")